/******************************************************************************
#ifdef DOC
File Name		: app_balance.c
Description		: Cell Balance
Remark			: -
Date			: 2019/05/17
Copyright		: Techsum Power Technology Co., Ltd.
#endif
******************************************************************************/
/****************************************************************************
 include files
*****************************************************************************/
#include "gd32f4xx.h"
#include "sys_type.h"
#include "bsp_usart.h"
#include "bsp_io.h"
#include "bsp_wdg.h"

#define RECV_POOL_SIZE	(100)

/****************************************************************************
 define macro
*****************************************************************************/
/****************************************************************************
 define type (Enumerated type ENUM)
*****************************************************************************/
/****************************************************************************
 Variable declaration
*****************************************************************************/
/* recv buffer */

UCHAR u8_gUartRevPool1[RECV_POOL_SIZE];
UCHAR u8_gUartRevPool2[RECV_POOL_SIZE];
ST_UART st_gUartSoc = {0};
ST_UART st_gUart232 = {0};


/****************************************************************************
 Prototype declaration
*****************************************************************************/
/******************************************************************************
#ifdef DOC
FUNCTION		: 
DESCRIPTION		: 
INPUT			: none
OUTPUT			: none
UPDATE			: -
DATE			: 2019/5/17
******************************************************************************/

void vBSP_USARTInit(uint32_t USARTx)
{
	if(USART_SOC == USARTx)
	{
		nvic_irq_enable(USART0_IRQn, 0, 0);
		rcu_periph_clock_enable( RCU_GPIOA);
		
		/* enable USART clock */
		rcu_periph_clock_enable(RCU_USART0);
		
		/* connect port to USARTx_Tx */
		gpio_af_set(GPIOA, GPIO_AF_7,GPIO_PIN_9);
		/* connect port to USARTx_Rx */
		gpio_af_set(GPIOA, GPIO_AF_7,GPIO_PIN_10);
		
		/* configure USART Tx as alternate function push-pull */
		gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP,GPIO_PIN_9);
		gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_9);
		/* configure USART Rx as alternate function push-pull */
		gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP,GPIO_PIN_10);
		gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_10);
		
		/* USART configure */
		usart_deinit(USARTx);
		usart_baudrate_set(USARTx,9600);
		usart_receive_config(USARTx, USART_RECEIVE_ENABLE);
		usart_transmit_config(USARTx, USART_TRANSMIT_ENABLE);
		usart_enable(USARTx);
		usart_interrupt_enable(USARTx, USART_INT_RBNE);
	}
	else if(USART_232 == USARTx)
	{
		nvic_irq_enable(USART2_IRQn, 0, 0);
		rcu_periph_clock_enable( RCU_GPIOB);
		
		/* enable USART clock */
		rcu_periph_clock_enable(RCU_USART2);
		
		/* connect port to USARTx_Tx */
		gpio_af_set(GPIOB, GPIO_AF_7,GPIO_PIN_10);
		/* connect port to USARTx_Rx */
		gpio_af_set(GPIOB, GPIO_AF_7,GPIO_PIN_11);
		
		/* configure USART Tx as alternate function push-pull */
		gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP,GPIO_PIN_10);
		gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_10);
		/* configure USART Rx as alternate function push-pull */
		gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP,GPIO_PIN_11);
		gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_11);
		
		/* USART configure */
		usart_deinit(USARTx);
		usart_baudrate_set(USARTx,9600);
		usart_receive_config(USARTx, USART_RECEIVE_ENABLE);
		usart_transmit_config(USARTx, USART_TRANSMIT_ENABLE);
		usart_enable(USARTx);
		usart_interrupt_enable(USARTx, USART_INT_RBNE);
	}


	st_gUartSoc.usRecvIdx = 0;
	st_gUartSoc.pRecvDataPool = u8_gUartRevPool1;
	
	st_gUart232.usRecvIdx = 0;
	st_gUart232.pRecvDataPool = u8_gUartRevPool2;
}


UCHAR ucBSP_UartSendDataPoll(uint32_t USARTx,UCHAR *pTxData, USHORT Size)
{
	for(USHORT idx = 0;idx<Size;idx++)
	{
      	vBSP_WDG_Refresh();
		while(RESET == usart_flag_get(USARTx, USART_FLAG_TBE));
      	usart_data_transmit(USARTx, (uint8_t)(*(pTxData+idx)));
	}
	vBSP_WDG_Refresh();
	while(RESET == usart_flag_get(USARTx, USART_FLAG_TC));
    return TRUE;
}


void UART_RxCpltCallback(uint32_t USARTx)
{
	UCHAR wk_rev_dat;
	wk_rev_dat = usart_data_receive(USARTx);

	if(USART_SOC == USARTx)
	{
		if(st_gUartSoc.usRecvIdx < RECV_POOL_SIZE)
		{
			st_gUartSoc.pRecvDataPool[st_gUartSoc.usRecvIdx] = wk_rev_dat;
			st_gUartSoc.usRecvIdx++;
		}
	}
	else if(USART_SOC == USARTx)
	{
		if(st_gUart232.usRecvIdx < RECV_POOL_SIZE)
		{
			st_gUart232.pRecvDataPool[st_gUart232.usRecvIdx] = wk_rev_dat;
			st_gUart232.usRecvIdx++;
		}
	}
}


